package com.iamteer.day08;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class Server {

	public static void main(String[] args) {
		try {
			//1. 创建 ServerSocket
			ServerSocket server = new ServerSocket(8888);
			System.out.println("服务器启动成功。。。");
			//2. 接收客户端的连接
			Socket socket = server.accept();
			
			//3. 获取本地文件
			FileInputStream in = new FileInputStream(new File("./src/test.html"));
			//4. 构建数据输出通道
			OutputStream out = socket.getOutputStream();
			byte[] buf = new byte[1024];
			int len = -1;
			while ((len = in.read(buf)) != -1) {
				System.out.println(new String(buf, 0, len));
				out.write(buf, 0, len);
			}
			out.close();
			in.close();
			
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}
